{smcl}
{com}{sf}{ul off}{txt}{.-}
      name:  {res}<unnamed>
       {txt}log:  {res}C:\Users\myogo\Princeton Dropbox\Motohiro Yogo\Koijen Yogo - International\JPE\Replication\Code\1 Data\Data4.smcl
  {txt}log type:  {res}smcl
 {txt}opened on:  {res}31 Oct 2025, 09:19:47
{txt}
{com}. /* Define local and global variables */
> 
> do global;
{txt}
{com}. /* global.do (STATA)
>         Define global variables in "Data".
>         by Ralph Koijen & Motohiro Yogo */
. 
. #delimit ;
{txt}delimiter now ;
{com}. clear all;
{res}{txt}
{com}. global year_min = 2003;
{txt}
{com}. global year_max = 2020;
{txt}
{com}. global small = 5e-4;
{txt}
{com}.         /* Minimum reportable threshold for CPIS */

{txt}end of do-file

{com}. /* Step 1: Merge characteristics and asset prices */
> 
> /* Load holdings data */
> 
> u Data3, clear;
{txt}
{com}. /* Merge investor country characteristics */
> 
> merge m:1 year country using Data0,
>         keepusing(Name MSCI Ieuro)
>         nogen keep(master match);
{res}
{txt}{col 5}Result{col 33}Number of obs
{col 5}{hline 41}
{col 5}Not matched{col 30}{res}           3,930
{txt}{col 9}from master{col 30}{res}           3,930{txt}  
{col 9}from using{col 30}{res}               0{txt}  

{col 5}Matched{col 30}{res}          65,857{txt}  
{col 5}{hline 41}

{com}. /* Rename variables */
> 
> foreach var of varlist Name MSCI Ieuro {c -(};
{txt}  2{com}.         rename `var' country_`var';
{txt}  3{com}. {c )-};
{res}{txt}
{com}. /* Merge asset prices */
> 
> merge m:1 year counterpart type using Data1,
>         keepusing(maturity yield mb _ret)
>         nogen keep(master match);
{res}{txt}(label {bf:{txt}type_label} already defined)

{col 5}Result{col 33}Number of obs
{col 5}{hline 41}
{col 5}Not matched{col 30}{res}           2,028
{txt}{col 9}from master{col 30}{res}           2,028{txt}  
{col 9}from using{col 30}{res}               0{txt}  

{col 5}Matched{col 30}{res}          67,759{txt}  
{col 5}{hline 41}

{com}. /* Merge issuer country characteristics */
> 
> merge m:1 year counterpart using Data0,
>         keepusing(Name MSCI Ieuro fx relpi realfx gdpP gdpPpc pop drate vol Gcpi _Gfx _Gcpi _GconsRpc)
>         nogen keep(master match);
{res}{txt}(label {bf:{txt}MSCI_label} already defined)

{col 5}Result{col 33}Number of obs
{col 5}{hline 41}
{col 5}Not matched{col 30}{res}           2,028
{txt}{col 9}from master{col 30}{res}           2,028{txt}  
{col 9}from using{col 30}{res}               0{txt}  

{col 5}Matched{col 30}{res}          67,759{txt}  
{col 5}{hline 41}

{com}. /* Merge GeoDist data */
> 
> merge m:1 country counterpart using GeoDist,
>         nogen keep(master match);
{res}
{txt}{col 5}Result{col 33}Number of obs
{col 5}{hline 41}
{col 5}Not matched{col 30}{res}           5,850
{txt}{col 9}from master{col 30}{res}           5,850{txt}  
{col 9}from using{col 30}{res}               0{txt}  

{col 5}Matched{col 30}{res}          63,937{txt}  
{col 5}{hline 41}

{com}. /* Step 2: Construct variables */
> 
> /* Fix variables */
> 
> replace country_Name = "Reserves" if country=="_CR";
{txt}(1,956 real changes made)

{com}. replace country_Name = "Other" if country=="_OC";
{txt}(1,974 real changes made)

{com}. recode country_Ieuro dist (. = 0);
{txt}(3,930 changes made to {bf:country_Ieuro})
(5,850 changes made to {bf:dist})

{com}. /* Define investor country group */
> 
> gen byte country_group = 0 if country=="_CR";
{txt}(67,831 missing values generated)

{com}. replace country_group = country_MSCI if inrange(country_MSCI,1,3);
{txt}(40,384 real changes made)

{com}. replace country_group = 4 if inrange(country_MSCI,4,6);
{txt}(25,473 real changes made)

{com}. replace country_group = 5 if inrange(country_MSCI,7,12) | country=="_OC";
{txt}(1,974 real changes made)

{com}. /* Define issuer country group */
> 
> gen byte group = MSCI if inrange(MSCI,1,3);
{txt}(27,266 missing values generated)

{com}. replace group = 4 if inrange(MSCI,4,6);
{txt}(25,238 real changes made)

{com}. gen counter_euro = counterpart;
{txt}
{com}. replace counter_euro = "EUR" if (Ieuro==1 | counterpart=="DNK") & type==1;
{txt}(6,627 real changes made)

{com}. replace counter_euro = "USA" if counterpart=="HKG" & type==1;
{txt}(539 real changes made)

{com}. /* Construct quantities */
> 
> gen Qamount = amount/(mb*fx);
{txt}(2,028 missing values generated)

{com}. egen market = total(amount), missing by(year counter_euro type);
{txt}
{com}. gen Qmarket = market/(mb*fx);
{txt}(2,028 missing values generated)

{com}. gen book = market/mb;
{txt}(2,028 missing values generated)

{com}. egen quant = total(amount/(mb*realfx)), missing by(year counterpart type);
{txt}(2,028 missing values generated)

{com}. /* Construct wealth */
> 
> egen wealth = total(amount), missing by(year country);
{txt}
{com}. egen wealthA = total(amount), missing by(year country type);
{txt}
{com}. egen wealth3 = total(amount*(type==3)), missing by(year country);
{txt}
{com}. egen outside = total(amount*(counterpart=="_OC")), missing by(year country);
{txt}
{com}. egen outsideA = total(amount*(counterpart=="_OC")), missing by(year country type);
{txt}
{com}. /* Drop other countries */
> 
> drop if counterpart=="_OC";
{txt}(2,028 observations deleted)

{com}. /* Construct ratios of wealth */
> 
> gen rw = amount/outsideA;
{txt}
{com}. egen Srw = total(rw), missing by(year country type);
{txt}
{com}. replace Srw = 1+Srw;
{txt}(67,759 real changes made)

{com}. gen rwealthA = wealthA/wealth3 if type!=3;
{txt}(23,779 missing values generated)

{com}. /* Construct log variables */
> 
> foreach var of varlist mb fx relpi realfx gdpP gdpPpc pop book quant rw rwealthA {c -(};
{txt}  2{com}.         gen L`var' = ln(`var');
{txt}  3{com}.         label var L`var' "log: `var'";
{txt}  4{com}. {c )-};
{txt}(23,779 missing values generated)

{com}. forval i = 1/2 {c -(};
{txt}  2{com}.         gen LSrw`i' = ln(Srw)*(type==`i') if type!=3;
{txt}  3{com}. {c )-};
{txt}(23,779 missing values generated)
(23,779 missing values generated)

{com}. sort year country type counterpart;
{txt}
{com}. by year country: gen LSrw3 = -ln(Srw[_N]) if type!=3 & type[_N]==3;
{txt}(23,779 missing values generated)

{com}. /* Construct dummy variables */
> 
> gen byte Iown = country==counterpart;
{txt}
{com}. gen byte I_CR = country=="_CR";
{txt}
{com}. gen byte I_OC = country=="_OC";
{txt}
{com}. gsort year country type -Iown counterpart;
{txt}
{com}. by year country type: gen byte Iycountry = _n==1;
{txt}
{com}. gsort year counterpart type -Iown country;
{txt}
{com}. by year counterpart type: gen byte Iycounterpart = _n==1;
{txt}
{com}. gen Iycounter_euro = Iycounterpart;
{txt}
{com}. replace Iycounter_euro = 0 if counterpart!="DEU" & counter_euro=="EUR";
{txt}(180 real changes made)

{com}. replace Iycounter_euro = 0 if counterpart=="HKG" & counter_euro=="USA";
{txt}(18 real changes made)

{com}. char year[omit] $year_max;
{txt}
{com}. char counterpart[omit] USA;
{txt}
{com}. char type[omit] 3;
{txt}
{com}. xi i.year i.counterpart i.type, pre(I);
{txt}i.year{col 19}Iyear_2003-2020{col 39}(naturally coded; Iyear_2020 omitted)
i.counterpart{col 19}Icounterpar_1-37{col 39}(Icounterpar_36 for count~t==USA omitted)
i.type{col 19}Itype_1-3{col 39}(naturally coded; Itype_3 omitted)

{com}. /* Construct lag variables */
> 
> foreach var of varlist Lmb Lfx {c -(};
{txt}  2{com}.         sort Iycounterpart counterpart type year;
{txt}  3{com}.         by Iycounterpart counterpart type: gen `var'_ = `var'[_n-1] if Iycounterpart;
{txt}  4{com}.         gsort year counterpart type -Iycounterpart;
{txt}  5{com}.         by year counterpart type: replace `var'_ = `var'_[1];
{txt}  6{com}. {c )-};
{txt}(65,950 missing values generated)
(62494 real changes made)
(65,950 missing values generated)
(62494 real changes made)

{com}. /* Construct portfolio flows */
> 
> egen int year_country = min(year), by(country);
{txt}
{com}. sort country counterpart type year;
{txt}
{com}. by country counterpart type: gen flow = amount/exp(Lmb-Lmb_+Lfx-Lfx_)-amount[_n-1] if year>year_country & year==year[_n-1]+1;
{txt}(5,116 missing values generated)

{com}. replace flow = amount/exp(Lmb-Lmb_+Lfx-Lfx_) if year>year_country & missing(flow);
{txt}(1,110 real changes made)

{com}. replace flow = amount if year>year_country & missing(flow);
{txt}(578 real changes made)

{com}. egen flowT = total(flow), missing by(year country);
{txt}(3,428 missing values generated)

{com}. drop Lmb_ Lfx_ year_country;
{txt}
{com}. /* Label variables */
> 
> order country_Name country_MSCI country_Ieuro country_group, after(country);
{txt}
{com}. order counter_euro Name MSCI Ieuro group, after(counterpart);
{txt}
{com}. label var country_Name          "Investor country name";
{txt}
{com}. label var country_group         "Investor country group";
{txt}
{com}. label var Name                          "Issuer country name";
{txt}
{com}. label var counter_euro          "Issuer country, Aggregate for euro ST debt";
{txt}
{com}. label var group                         "Issuer country group";
{txt}
{com}. label var amount                        "Investment amount (US$ billion)";
{txt}
{com}. label var Qamount                       "Investment quantity (LCU billion)";
{txt}
{com}. label var market                        "Market value, Aggregate for euro ST debt (US$ billion)";
{txt}
{com}. label var Qmarket                       "Market quantity, Aggregate for euro ST debt (LCU billion)";
{txt}
{com}. label var book                          "Book value, Aggregate for euro ST debt (US$ billion)";
{txt}
{com}. label var quant                         "PPP-adjusted quantity (US$ billion)";
{txt}
{com}. label var wealth                        "Wealth (US$ billion)";
{txt}
{com}. label var wealthA                       "Wealth by asset type (US$ billion)";
{txt}
{com}. label var wealth3                       "Wealth in equity (US$ billion)";
{txt}
{com}. label var outside                       "Outside wealth (US$ billion)";
{txt}
{com}. label var outsideA                      "Outside wealth by asset type (US$ billion)";
{txt}
{com}. label var rw                            "Ratio: amount to outsideA";
{txt}
{com}. label var Srw                           "Sum of rw by asset type";
{txt}
{com}. label var rwealthA                      "Ratio: wealthA to wealth3";
{txt}
{com}. label var LSrw1                         "log: Srw for ST debt";
{txt}
{com}. label var LSrw2                         "log: Srw for LT debt";
{txt}
{com}. label var LSrw3                         "log: Srw for equity";
{txt}
{com}. label var Iown                          "Dummy: Own country";
{txt}
{com}. label var I_CR                          "Dummy: Currency reserves";
{txt}
{com}. label var I_OC                          "Dummy: Other countries";
{txt}
{com}. label var Iycountry                     "Dummy: Unique sample by year, country, type";
{txt}
{com}. label var Iycounterpart         "Dummy: Unique sample by year, counterpart, type";
{txt}
{com}. label var Iycounter_euro        "Dummy: Unique sample by year, counter_euro, type";
{txt}
{com}. label var flow                          "Portfolio flow (US$ billion)";
{txt}
{com}. label var flowT                         "Total portfolio flow (US$ billion)";
{txt}
{com}. label define group_label
>         0 "Currency reserves"
>         1 "DM: Americas"
>         2 "DM: Europe & Middle East"
>         3 "DM: Pacific"
>         4 "EM"
>         5 "Other";
{txt}
{com}. label val country_group group group_label;
{txt}
{com}. /* Save data */
> 
> sort year country counterpart type;
{txt}
{com}. save Data4, replace;
{txt}{p 0 4 2}
file {bf}
Data4.dta{rm}
saved
{p_end}

{com}. log close;
      {txt}name:  {res}<unnamed>
       {txt}log:  {res}C:\Users\myogo\Princeton Dropbox\Motohiro Yogo\Koijen Yogo - International\JPE\Replication\Code\1 Data\Data4.smcl
  {txt}log type:  {res}smcl
 {txt}closed on:  {res}31 Oct 2025, 09:19:49
{txt}{.-}
{smcl}
{txt}{sf}{ul off}